package queries;

import controller.LollaWrapper;
import entities.Venue;
import entities.Venues;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class VenueQueries extends AbstractQueries {
    private long pageCount;
    private final String baseVenueUrl;
    private final LollaWrapper wrap;

    public VenueQueries(LollaWrapper wrap) throws IOException {
        this.wrap = wrap;
        baseVenueUrl = wrap.createFullAPIUrl("venues");
        Venues venues = wrap.makeCall(baseVenueUrl, Venues.class);
        pageCount = venues.getTotalPages();
    }

    private List<Venue> getVenuesByPage(int page) throws IOException {
        String url = baseVenueUrl + "&page=" + page;
        return wrap.makeCall(url, Venues.class).getVenues();
    }

    public List<Venue> getAllVenues() throws IOException {
        List<Venue> allVenues = new ArrayList<Venue>();
        for (int i = 1; i <= pageCount; i++) {
            allVenues.addAll(getVenuesByPage(i));
        }
        return allVenues;
    }

    public Venue getVenueById(long id) throws IOException {
        return wrap.makeCall(wrap.createFullAPIUrl("venues/" + id), Venue.class);
    }

}
